/*
 * File Name: ConnectionHelper.java
 * Created on: November 9, 2007, 4:24 PM
 */
package co.edu.uniandes.intercon.server.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Contains utility methods used to manage <code>input/output</code> resources
 * 
 * @author Fabian Gutierrez [fagossa@gmail.com]
 */
public class ConnectionHelper {

	private static final String ERROR_CLOSING_RESOURCE = "Error closing resource";

	/**
	 * Close the resources specified
	 * 
	 * @param oos
	 *            <code>ObjectOutputStream</code> instance to be closed
	 * @param ois
	 *            <code>ObjectInputStream</code> instance to be closed
	 * @param socket
	 *            <code>Socket</code> instance to be closed
	 */
	public static void close(ObjectOutputStream oos, ObjectInputStream ois,
			Socket socket) {
		close(oos);
		close(ois);
		close(socket);
	}

	/**
	 * Close the resources specified
	 * 
	 * @param in
	 *            InputStream to be closed
	 * @param out
	 *            OutputStream to be closed
	 * @param socket
	 *            Socked client to be closed
	 */
	public static void close(ObjectInputStream in, ObjectOutputStream out,
			Socket socket) {
		close(in);
		close(out);
		close(socket);
	}

	/**
	 * Close a <code>RandomAccesFile</code> resource. If this resource is
	 * <code>null</code>, it is just ignored
	 * 
	 * @param file
	 *            resource to be closed
	 */
	public static void closeResource(RandomAccessFile file) {
		if (file != null) {
			try {
				file.close();
			} catch (IOException ex) {
				P2PLogger.logError(ERROR_CLOSING_RESOURCE, ex);
			}
		}
	}

	/**
	 * Closes a Object Output Stream. If the <code>ObjectOutputStream</code>
	 * instance is <code>null</code>, it is just ignored
	 * 
	 * @param obj
	 *            ObjectOutputStrem to be closed
	 */
	public static void close(ObjectOutputStream obj) {
		if (obj != null) {
			try {
				obj.close();
			} catch (IOException ex) {
				P2PLogger.logError(ERROR_CLOSING_RESOURCE, ex);
			}
		}
	}

	/**
	 * Closes a socket client. If the <code>ObjectInputStream</code> instance is
	 * <code>null</code>, it is just ignored
	 * 
	 * @param obj
	 *            Object Input Stream to be closed
	 */
	public static void close(ObjectInputStream obj) {
		if (obj != null) {
			try {
				obj.close();
			} catch (IOException ex) {
				P2PLogger.logError(ERROR_CLOSING_RESOURCE, ex);
			}
		}
	}

	/**
	 * Closes a socket client. If the socket client is <code>null</code> is just
	 * ignored
	 * 
	 * @param obj
	 *            socket to be closed
	 */
	public static void close(Socket obj) {
		if (obj != null) {
			try {
				obj.close();
			} catch (IOException ex) {
				P2PLogger.logError(ERROR_CLOSING_RESOURCE, ex);
			}
		}
	}

	/**
	 * Closes an <code>InputStream</code>. If the <code>InputStream</code>
	 * instance is <code>null</code>, it is just ignored
	 * 
	 * @param is
	 *            Stream to be closed
	 */
	public static void close(InputStream is) {
		if (is != null) {
			try {
				is.close();
			} catch (IOException ex) {
				P2PLogger.logError(ERROR_CLOSING_RESOURCE, ex);
			}
		}
	}

	/**
	 * Closes an <code>OutputStream</code>. If the <code>OutputStream</code>
	 * instance is <code>null</code>, it is just ignored
	 * 
	 * @param out
	 *            Stream to be closed
	 */
	public static void close(OutputStream out) {
		if (out != null) {
			try {
				out.close();
			} catch (IOException ex) {
				P2PLogger.logError(ERROR_CLOSING_RESOURCE, ex);
			}
		}
	}

	/**
	 * Retrieves the current host
	 * 
	 * @return ip address of the current host
	 */
	public static String getCurrentHost() {
		try {
			return InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			return "";
		}
	}
	
	/**
	 * Close the resource specified
	 * @param	out	Resource to be closed
	 */
	public static void close(MulticastSocket out) {
		if (out != null) {
			out.close();
		}
	}
}
